package br.com.hotel68.view.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextArea;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import br.com.hotel68.Hotel68BusinessFacade;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JProgressBar;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ClientWindow extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField nameClient;
	private JLabel labelFeedBack;
	private JProgressBar progressBar;

	

	/**
	 * Create the dialog.
	 */
	public ClientWindow() {
		setBounds(100, 100, 448, 193);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNome = new JLabel("Nome Cliente:");
			lblNome.setBounds(10, 13, 102, 14);
			contentPanel.add(lblNome);
		}
		{
			nameClient = new JTextField();
			nameClient.addFocusListener(new FocusAdapter() {
				public void focusGained(FocusEvent arg0) {
					cleanNameClient();
				}
			});
			nameClient.setBounds(141, 10, 281, 20);
			contentPanel.add(nameClient);
			nameClient.setColumns(30);
		}
		
		labelFeedBack = new JLabel("");
		labelFeedBack.setEnabled(false);
		labelFeedBack.setBounds(10, 48, 323, 14);
		contentPanel.add(labelFeedBack);
		
		progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		progressBar.setEnabled(false);
		progressBar.setBounds(10, 108, 102, 14);
		contentPanel.add(progressBar);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Salvar");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						saveClient();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelWindow();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	
	
	
	public void saveClient() {
		try {
			if(validaClient()) {
				new Hotel68BusinessFacade(null).saveClient(nameClient.getText());
				this.progressBar.setValue(100);
				this.cancelWindow();
			}
		} catch (Exception e1) {
			 TextArea texto = new TextArea();
			 texto.setEditable(false);
			 texto.setText("Caro usuário, ocorreu um erro inesperado, pedimos a gentileza que tente novamente, " +
					 "caso o erro persista entre em contato com o Administrador do sistema.\n\n" + e1.getMessage());
			JOptionPane.showMessageDialog(null, texto);
		}
	}
	
	public void cancelWindow() {
		this.setVisible(false);
	}
	
	public boolean validaClient() throws Exception{
		if(this.nameClient.getText().trim().equalsIgnoreCase("")){
			this.labelFeedBack.setText("Informe o nome do cliente");
			this.nameClient.setBackground(Color.red);
			this.nameClient.setFocusable(true);
			return false;
		} else {
			return true;
		}
	}
	
	public void cleanNameClient() {
		this.labelFeedBack.setText(" ");
		this.nameClient.setBackground(Color.WHITE);
	}
	
}
